﻿-- =============================================
-- Author:		Jon Stocksdale
-- Create date: May 10, 2016
-- Description:	Return Parsed out Relationship With Direction
--    PASS:  ('PARENT.forward','Direction)
--    RETURN:  forward
--    PASS: ('PARENT.forward','Relationship')
--    RETURN:  PARENT
-- =============================================
CREATE FUNCTION [dbo].[fn_GetRelOrDir]
(
	-- Add the parameters for the function here
	@SRNameWithDir nvarchar(100),
	@ReturnWhat nvarchar(15)
)
RETURNS nvarchar(100)
AS
BEGIN
	DECLARE @RelName nvarchar(100)
	DECLARE @Direction nvarchar(15)
	DECLARE @RETVAL nvarchar(100)
	IF CHARINDEX('.',@SRNameWithDir) <> 0 BEGIN
		SET @RelName = SUBSTRING(@SRNameWithDir,1,CHARINDEX('.',@SRNameWithDir)-1)
		SET @Direction = SUBSTRING(@SRNameWithDir,CHARINDEX('.',@SRNameWithDir)+1,LEN(@SRNameWithDir))
	END
	ELSE BEGIN 
		SET @RelName = @SRNameWithDir
		SET @Direction = NULL
	END

	IF (SUBSTRING(@ReturnWhat,1,3) = 'Rel')
			SET @RETVAL = @RelName
	ELSE
			SET @RETVAL = @Direction

	RETURN @RETVAL

END